﻿/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2/18/2011
 * Time: 8:29 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Web.Script.Serialization;
using System.Diagnostics;
using HtmlAgilityPack;
using TomatoSynchronizer.Store;
using TomatoSynchronizer.Forms;
using FileSystemDatabase;
using TomatoSynchronizer.UI;
using TomatoSynchronizer.Entities;
using TomatoSynchronizer.Synchronizer;

namespace TomatoSynchronizer.Forms
{
	/// <summary>
	/// Description of SyncForm.
	/// </summary>
	public partial class SyncForm : Form
	{
		JsonDatabase db = new JsonDatabase("sites");
		HtmlWeb _client;
		Site[] _sites;
		
		public SyncForm()
		{
			db = new JsonDatabase("sites");
			_client = new HtmlWeb();
			
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			LoadSites();
		}
		
		private void LoadSites()
		{
			_sites  = db.Read<Site>();
			
			comboBoxSrc.Items.Clear();
			comboBoxDest.Items.Clear();
			
			foreach(Site site in _sites)
			{
				comboBoxSrc.Items.Add(site.Name);
				comboBoxDest.Items.Add(site.Name);
			}
		}
		
		void BtnNewSiteClick(object sender, EventArgs e)
		{
			SiteConfigForm frm = new SiteConfigForm();
			frm.ShowDialog();
			LoadSites();
		}
		
		void BtnCompareClick(object sender, EventArgs e)
		{
			Site siteSrc = _sites[comboBoxSrc.SelectedIndex];
			Site siteDest = _sites[comboBoxDest.SelectedIndex];
			
			if(siteSrc.Login(_client) && siteDest.Login(_client))
			{
				treeViewSrc.Nodes.Clear();
				treeViewDest.Nodes.Clear();
				
				DownloadLanguages(siteSrc,_client,treeViewSrc);
				DownloadLanguages(siteDest,_client,treeViewDest);

				DownloadDevises(siteSrc,_client,treeViewSrc);
				DownloadDevises(siteDest,_client,treeViewDest);
				
				DownloadWeightClasses(siteSrc,_client,treeViewSrc);
				DownloadWeightClasses(siteDest,_client,treeViewDest);
				
				DownloadUnitClasses(siteSrc,_client,treeViewSrc);
				DownloadUnitClasses(siteDest,_client,treeViewDest);
				
				DownloadArticleCartegories(siteSrc,_client,treeViewSrc);
				DownloadArticleCartegories(siteDest,_client,treeViewDest);
				
				DownloadArticles(siteSrc,_client,treeViewSrc);
				DownloadArticles(siteDest,_client,treeViewDest);
				
				DownloadInformations(siteSrc,_client,treeViewSrc);
				DownloadInformations(siteDest,_client,treeViewDest);
				
				DownloadFaqs(siteSrc,_client,treeViewSrc);
				DownloadFaqs(siteDest,_client,treeViewDest);
				
				DownLoadCategories(siteSrc,_client,treeViewSrc);
				DownLoadCategories(siteDest,_client,treeViewDest);
				
				DownloadSlideImages(siteSrc,_client,treeViewSrc);
				DownloadSlideImages(siteDest,_client,treeViewDest);

				DownloadGuestBooks(siteSrc,_client,treeViewSrc);
				DownloadGuestBooks(siteDest,_client,treeViewDest);
//			DownloadConfiguration(siteSrc,_client,treeViewSrc);
//			DownloadConfiguration(siteDest,_client,treeViewDest);
//
//			DownloadProductVariantGroup(siteSrc,_client,treeViewSrc);
//			DownloadProductVariantGroup(siteDest,_client,treeViewDest);
//
//			DownloadAttributGroup(siteSrc,_client,treeViewSrc);
//			DownloadAttributGroup(siteDest,_client,treeViewDest);
//
//			DownloadCustomerGroups(siteSrc,_client,treeViewSrc);
//			DownloadCustomerGroups(siteDest,_client,treeViewDest);
//
//			DownloadQuantityDiscountGroups(siteSrc,_client,treeViewSrc);
//			DownloadQuantityDiscountGroups(siteDest,_client,treeViewDest);
//
//			DownloadManufacturers(siteSrc,_client,treeViewSrc);
//			DownloadManufacturers(siteDest,_client,treeViewDest);
//
//			DownloadSpecials(siteSrc,_client,treeViewSrc);
//			DownloadSpecials(siteDest,_client,treeViewDest);
//
//			DownloadProductAttachments(siteSrc,_client,treeViewSrc);
//			DownloadProductAttachments(siteDest,_client,treeViewDest);
//
//			DownloadProducts(siteSrc,_client,treeViewSrc);
//			DownloadProducts(siteDest,_client,treeViewDest);
//
//			DownloadPolls(siteSrc,_client,treeViewSrc);
//			DownloadPolls(siteDest,_client,treeViewDest);
//
//			DownloadCustomers(siteSrc,_client,treeViewSrc);
//			DownloadCustomers(siteDest,_client,treeViewDest);
//
//			DownloadOrders(siteSrc,_client,treeViewSrc);
//			DownloadOrders(siteDest,_client,treeViewDest);
//
//			DownloadCoupons(siteSrc,_client,treeViewSrc);
//			DownloadCoupons(siteDest,_client,treeViewDest);
//
//			DownloadUsers(siteSrc,_client,treeViewSrc);
//			DownloadUsers(siteDest,_client,treeViewDest);
				
				BtnSynchronize.Enabled = true;
			}
			else
			{
				MessageBox.Show("Could not login");
				return;
			}
		}
		
		private void DownloadUsers(Site site,HtmlWeb client,TreeView treeView)
		{
			ReplicaNode UsersNode = new ReplicaNode("Users");
			UsersNode.Name = "Users";
			treeView.Nodes.Add(UsersNode);
			
			UserStore store = new UserStore(site,client);
			List<Dictionary<string,object>> Users = store.GetUsers();
			int i = 1;
			foreach(Dictionary<string,object> user in Users)
			{
				ReplicaNode userNode = new ReplicaNode(user["user_name"].ToString());
				userNode.Name = user["id"].ToString();
				UsersNode.Nodes.Add(userNode);
				
				foreach(string key in user.Keys)
				{
					if(key != "accesses")
					{
						TreeNode entryKeyNode = new TreeNode(key + " : " + user[key]);
						entryKeyNode.Name = key;
						userNode.Nodes.Add(entryKeyNode);
					}
				}
				
				foreach(string key1 in user.Keys)
				{
					if(key1 == "accesses")
					{
						ReplicaNode accessesNode = new ReplicaNode("accesses");
						accessesNode.Name = "accesses";
						userNode.Nodes.Add(accessesNode);
						
						List<Dictionary<string,object>> accesses = (List<Dictionary<string,object>>)user[key1];
						
						foreach(Dictionary<string,object> access in accesses)
						{
							ReplicaNode accessNode = new ReplicaNode(access["text"].ToString());
							accessNode.Name = access["id"].ToString();
							accessesNode.Nodes.Add(accessNode);
							
							foreach(string cle1 in access.Keys)
							{
								if(cle1 != "children")
								{
									TreeNode entryKeyNode = new TreeNode(cle1 + " : " + access[cle1]);
									entryKeyNode.Name = cle1;
									accessNode.Nodes.Add(entryKeyNode);
								}
							}
							
							foreach(string cle2 in access.Keys)
							{
								if(cle2 == "children")
								{
									ReplicaNode childrenNode = new ReplicaNode("children");
									childrenNode.Name = "children";
									accessNode.Nodes.Add(childrenNode);
									
									List<Dictionary<string,object>> children = (List<Dictionary<string,object>>)access[cle2];
									
									foreach(Dictionary<string,object> child in children)
									{
										ReplicaNode childNode = new ReplicaNode(child["text"].ToString());
										childNode.Name = child["id"].ToString();
										childrenNode.Nodes.Add(childNode);
										
										foreach(string cle3 in child.Keys)
										{
											TreeNode entryKeyNode = new TreeNode(cle3 + " : " + child[cle3]);
											entryKeyNode.Name = cle3;
											childNode.Nodes.Add(entryKeyNode);
										}
									}
								}
							}
						}
					}
				}
				
				store.PersistUser(user);
				
				i++;
			}
			
			store.PersistUsers(Users);
		}
		
		private void DownloadDevises(Site site,HtmlWeb client,TreeView treeView)
		{
			ReplicaNode DevisesNode = new ReplicaNode("Devises");
			DevisesNode.Name = "Devises";
			treeView.Nodes.Add(DevisesNode);
			
			DeviseStore store = new DeviseStore(site,client);
			List<Devise> Devises = store.GetItems();
			
			foreach(Devise devise in Devises)
			{
				ReplicaNode DeviseNode = new ReplicaNode(devise.title);
				DeviseNode.Name = devise.title;
				DevisesNode.Nodes.Add(DeviseNode);
			}
		}
		
		private void DownloadArticleCartegories(Site site,HtmlWeb client,TreeView treeView)
		{
			ReplicaNode InformationsNode = new ReplicaNode("Articles Categories");
			InformationsNode.Name = "ArticlesCategories";
			treeView.Nodes.Add(InformationsNode);
			
			ArticleCategoryStore store = new ArticleCategoryStore(site,client);
			List<ArticleCategory> ArticleCartegories = store.GetItems();
			int i = 1;
			foreach(ArticleCategory category in ArticleCartegories)
			{
				ReplicaNode ArticleCategoryNode = new ReplicaNode(category.articles_categories_name);
				ArticleCategoryNode.Name = category.articles_categories_name;
				InformationsNode.Nodes.Add(ArticleCategoryNode);
				
				i++;
			}
		}
		
		private void DownloadInformations(Site site,HtmlWeb client,TreeView treeView)
		{
			ReplicaNode InformationsNode = new ReplicaNode("Informations");
			InformationsNode.Name = "Informations";
			treeView.Nodes.Add(InformationsNode);
			
			InformationStore store = new InformationStore(site,client);
			List<Information> Informations = store.GetItems();
			int i = 1;
			foreach(Information infos in Informations)
			{
				ReplicaNode InformationNode = new ReplicaNode(infos.articles_name);
				InformationNode.Name = infos.articles_id;
				InformationsNode.Nodes.Add(InformationNode);
				
				i++;
			}
		}
		
		private void DownloadWeightClasses(Site site,HtmlWeb client,TreeView treeView)
		{
			ReplicaNode WeightClassesNode = new ReplicaNode("WeightClasses");
			WeightClassesNode.Name = "WeightClasses";
			treeView.Nodes.Add(WeightClassesNode);
			
			WeightClassStore store = new WeightClassStore(site,client);
			List<WeightClass> WeightClasses = store.GetItems();
			int i = 1;
			foreach(WeightClass weightClass in WeightClasses)
			{
				ReplicaNode WeightClassNode = new ReplicaNode(weightClass.weight_class_title);
				WeightClassNode.Name = weightClass.weight_class_id;
				WeightClassesNode.Nodes.Add(WeightClassNode);
				
				i++;
			}
		}
		
		private void DownloadLanguages(Site site,HtmlWeb client,TreeView treeView)
		{
			ReplicaNode LanguagesNode = new ReplicaNode("Languages");
			LanguagesNode.Name = "Languages";
			treeView.Nodes.Add(LanguagesNode);
			
			LanguageStore store = new LanguageStore(site,client);
			List<Language> Languages = store.GetItems();
			int i = 1;
			foreach(Language language in Languages)
			{
				ReplicaNode LanguageNode = new ReplicaNode(language.name);
				LanguageNode.Name = language.languages_id;
				LanguagesNode.Nodes.Add(LanguageNode);
				
				i++;
			}
		}
		
		private void DownloadUnitClasses(Site site,HtmlWeb client,TreeView treeView)
		{
			ReplicaNode UnitClassesNode = new ReplicaNode("UnitClasses");
			UnitClassesNode.Name = "UnitClasses";
			treeView.Nodes.Add(UnitClassesNode);
			
			UnitClassStore store = new UnitClassStore(site,client);
			List<UnitClass> UnitClasses = store.GetItems();
			int i = 1;
			foreach(UnitClass uclass in UnitClasses)
			{
				ReplicaNode UnitClassNode = new ReplicaNode(uclass.unit_class_titles["unit_class_title[" + uclass.language_id + "]"].ToString());
				UnitClassNode.Name = uclass.unit_class_id;
				UnitClassesNode.Nodes.Add(UnitClassNode);
				
				i++;
			}
		}
		
		private void DownloadCoupons(Site site,HtmlWeb client,TreeView treeView)
		{
			ReplicaNode CouponsNode = new ReplicaNode("Coupons");
			CouponsNode.Name = "Coupons";
			treeView.Nodes.Add(CouponsNode);
			
			CouponStore store = new CouponStore(site,client);
			List<Dictionary<string,object>> Coupons = store.GetCoupons();
			int i = 1;
			foreach(Dictionary<string,object> Coupon in Coupons)
			{
				ReplicaNode CouponNode = new ReplicaNode(Coupon["coupons_name"].ToString());
				CouponNode.Name = Coupon["coupons_id"].ToString();
				CouponsNode.Nodes.Add(CouponNode);
				
				foreach(string key in Coupon.Keys)
				{
					if(key != "categories" && key != "products")
					{
						TreeNode entryKeyNode = new TreeNode(key + " : " + Coupon[key]);
						entryKeyNode.Name = key;
						CouponNode.Nodes.Add(entryKeyNode);
					}
				}
				
				foreach(string key in Coupon.Keys)
				{
					if(key == "categories")
					{
						ReplicaNode categoriesNode = new ReplicaNode("categories");
						categoriesNode.Name = "categories";
						CouponNode.Nodes.Add(categoriesNode);
						
						List<Dictionary<string,object>> categories = (List<Dictionary<string,object>>)Coupon[key];
						
						foreach(Dictionary<string,object> category in categories)
						{
							ReplicaNode categoryNode = new ReplicaNode(category["name"].ToString());
							categoryNode.Name = category["id"].ToString();
							categoriesNode.Nodes.Add(categoryNode);
							
							foreach(string cle in category.Keys)
							{
								TreeNode entryKeyNode = new TreeNode(cle + " : " + category[cle]);
								entryKeyNode.Name = key;
								categoryNode.Nodes.Add(entryKeyNode);
							}
						}
					}
				}
				
				foreach(string key in Coupon.Keys)
				{
					if(key == "products")
					{
						ReplicaNode productsNode = new ReplicaNode("products");
						productsNode.Name = "products";
						CouponNode.Nodes.Add(productsNode);
						
						List<Dictionary<string,object>> products = (List<Dictionary<string,object>>)Coupon[key];
						
						foreach(Dictionary<string,object> product in products)
						{
							ReplicaNode productNode = new ReplicaNode(product["name"].ToString());
							productNode.Name = product["id"].ToString();
							productsNode.Nodes.Add(productNode);
							
							foreach(string cle in product.Keys)
							{
								TreeNode entryKeyNode = new TreeNode(cle + " : " + product[cle]);
								entryKeyNode.Name = key;
								productNode.Nodes.Add(entryKeyNode);
							}
						}
					}
				}
				
				store.PersistCoupon(Coupon);
				
				i++;
			}
			
			store.PersistCoupons(Coupons);
		}
		
		private void DownloadOrders(Site site,HtmlWeb client,TreeView treeView)
		{
			ReplicaNode OrdersNode = new ReplicaNode("Orders");
			OrdersNode.Name = "Orders";
			treeView.Nodes.Add(OrdersNode);
			
			OrderStore store = new OrderStore(site,client);
			List<Dictionary<string,object>> Orders = store.GetOrders();
			int i = 1;
			foreach(Dictionary<string,object> Order in Orders)
			{
				ReplicaNode OrderNode = new ReplicaNode(Order["orders_id"].ToString());
				OrderNode.Name = Order["orders_id"].ToString();
				OrdersNode.Nodes.Add(OrderNode);
				
				foreach(string key in Order.Keys)
				{
					if(key != "order_products" && key != "orders_status" && key != "gift_certificates")
					{
						TreeNode entryKeyNode = new TreeNode(key + " : " + Order[key]);
						entryKeyNode.Name = key;
						OrderNode.Nodes.Add(entryKeyNode);
					}
				}
				
				foreach(string key in Order.Keys)
				{
					if(key == "order_products")
					{
						ReplicaNode order_productsNode = new ReplicaNode("order_products");
						order_productsNode.Name = "order_products";
						OrderNode.Nodes.Add(order_productsNode);
						
						List<Dictionary<string,object>> order_products = (List<Dictionary<string,object>>)Order[key];
						
						foreach(Dictionary<string,object> order_product in order_products)
						{
							ReplicaNode order_productNode = new ReplicaNode(order_product["products_id"].ToString());
							order_productNode.Name = order_product["products_id"].ToString();
							order_productsNode.Nodes.Add(order_productNode);
							
							foreach(string cle in order_product.Keys)
							{
								TreeNode entryKeyNode = new TreeNode(cle + " : " + order_product[cle]);
								entryKeyNode.Name = cle;
								order_productNode.Nodes.Add(entryKeyNode);
							}
						}
					}
				}
				
				foreach(string key in Order.Keys)
				{
					if(key == "orders_status")
					{
						ReplicaNode orders_statusNode = new ReplicaNode("orders_status");
						orders_statusNode.Name = "orders_status";
						OrderNode.Nodes.Add(orders_statusNode);
						
						List<Dictionary<string,object>> orders_status = (List<Dictionary<string,object>>)Order[key];
						
						foreach(Dictionary<string,object> status in orders_status)
						{
							ReplicaNode statusNode = new ReplicaNode(status["status"].ToString());
							statusNode.Name = status["orders_status_history_id"].ToString();
							orders_statusNode.Nodes.Add(statusNode);
							
							foreach(string cle in status.Keys)
							{
								TreeNode entryKeyNode = new TreeNode(cle + " : " + status[cle]);
								entryKeyNode.Name = cle;
								statusNode.Nodes.Add(entryKeyNode);
							}
						}
					}
				}
				
				foreach(string key in Order.Keys)
				{
					if(key == "gift_certificates")
					{
						ReplicaNode gift_certificatesNode = new ReplicaNode("gift_certificates");
						gift_certificatesNode.Name = "gift_certificates";
						OrderNode.Nodes.Add(gift_certificatesNode);
						
						List<Dictionary<string,object>> gift_certificates = (List<Dictionary<string,object>>)Order[key];
						
						foreach(Dictionary<string,object> gift_certificate in gift_certificates)
						{
							ReplicaNode gift_certificateNode = new ReplicaNode(gift_certificate["status_name"].ToString());
							gift_certificateNode.Name = gift_certificate["status_id"].ToString();
							gift_certificatesNode.Nodes.Add(gift_certificateNode);
							
							foreach(string cle in gift_certificate.Keys)
							{
								TreeNode entryKeyNode = new TreeNode(cle + " : " + gift_certificate[cle]);
								entryKeyNode.Name = cle;
								gift_certificateNode.Nodes.Add(entryKeyNode);
							}
						}
					}
				}
				
				store.PersistOrder(Order);
				
				i++;
			}
			
			store.PersistOrders(Orders);
		}
		
		private void DownloadCustomers(Site site,HtmlWeb client,TreeView treeView)
		{
			ReplicaNode CustomersNode = new ReplicaNode("Customers");
			CustomersNode.Name = "Customers";
			treeView.Nodes.Add(CustomersNode);
			
			CustomerStore store = new CustomerStore(site,client);
			List<Dictionary<string,object>> Customers = store.GetCustomers();
			
			int i = 1;
			foreach(Dictionary<string,object> Customer in Customers)
			{
				ReplicaNode CustomerNode = new ReplicaNode(Customer["customers_firstname"].ToString() + " " + Customer["customers_lastname"].ToString());
				CustomerNode.Name = Customer["customers_id"].ToString();
				CustomersNode.Nodes.Add(CustomerNode);
				
				foreach(string key in Customer.Keys)
				{
					if(key != "address_books" && key != "store_credits" && key != "wishlists")
					{
						TreeNode entryKeyNode = new TreeNode(key + " : " + Customer[key]);
						entryKeyNode.Name = key;
						CustomerNode.Nodes.Add(entryKeyNode);
					}
				}
				
				foreach(string key in Customer.Keys)
				{
					if(key == "address_books")
					{
						ReplicaNode address_booksNode = new ReplicaNode("address_books");
						address_booksNode.Name = "address_books";
						CustomerNode.Nodes.Add(address_booksNode);
						
						List<Dictionary<string,object>> address_books = (List<Dictionary<string,object>>)Customer[key];
						
						foreach(Dictionary<string,object> address_book in address_books)
						{
							ReplicaNode address_bookNode = new ReplicaNode(address_book["address_book_id"].ToString() + " " + Customer["customers_id"].ToString());
							address_bookNode.Name = address_book["address_book_id"].ToString();
							address_booksNode.Nodes.Add(address_bookNode);
							
							foreach(string cle in address_book.Keys)
							{
								TreeNode entryKeyNode = new TreeNode(cle + " : " + address_book[cle]);
								entryKeyNode.Name = cle;
								address_bookNode.Nodes.Add(entryKeyNode);
							}
						}
					}
				}
				
				foreach(string key1 in Customer.Keys)
				{
					if(key1 == "store_credits")
					{
						ReplicaNode store_creditsNode = new ReplicaNode("store_credits");
						store_creditsNode.Name = "store_credits";
						CustomerNode.Nodes.Add(store_creditsNode);
						
						List<Dictionary<string,object>> store_credits = (List<Dictionary<string,object>>)Customer[key1];
						
						foreach(Dictionary<string,object> store_credit in store_credits)
						{
							ReplicaNode store_creditNode = new ReplicaNode(store_credit["address_book_id"].ToString() + " " + Customer["customers_id"].ToString());
							store_creditNode.Name = store_credit["address_book_id"].ToString();
							store_creditsNode.Nodes.Add(store_creditNode);
							
							foreach(string cle in store_credit.Keys)
							{
								TreeNode entryKeyNode = new TreeNode(cle + " : " + store_credit[cle]);
								entryKeyNode.Name = cle;
								store_creditNode.Nodes.Add(entryKeyNode);
							}
						}
					}
				}
				
				foreach(string key2 in Customer.Keys)
				{
					if(key2 == "wishlists")
					{
						ReplicaNode wishlistsNode = new ReplicaNode("wishlists");
						wishlistsNode.Name = "wishlists";
						CustomerNode.Nodes.Add(wishlistsNode);
						
						List<Dictionary<string,object>> wishlists = (List<Dictionary<string,object>>)Customer[key2];
						
						foreach(Dictionary<string,object> wishlist in wishlists)
						{
							ReplicaNode wishlistNode = new ReplicaNode(wishlist["products_name"].ToString() + " " + Customer["customers_id"].ToString());
							wishlistNode.Name = wishlist["wishlists_products_id"].ToString();
							wishlistsNode.Nodes.Add(wishlistNode);
							
							foreach(string cle in wishlist.Keys)
							{
								TreeNode entryKeyNode = new TreeNode(cle + " : " + wishlist[cle]);
								entryKeyNode.Name = cle;
								wishlistNode.Nodes.Add(entryKeyNode);
							}
						}
					}
				}
				
				store.PersistCustomer(Customer);
				
				i++;
			}
			
			store.PersistCustomers(Customers);
		}
		
		private void DownloadPolls(Site site,HtmlWeb client,TreeView treeView)
		{
			ReplicaNode PollsNode = new ReplicaNode("Polls");
			PollsNode.Name = "Polls";
			treeView.Nodes.Add(PollsNode);
			
			PollStore store = new PollStore(site,client);
			List<Dictionary<string,object>> Polls = store.GetPolls();
			int i = 1;
			foreach(Dictionary<string,object> Poll in Polls)
			{
				ReplicaNode PollNode = new ReplicaNode(Poll["polls_id"].ToString());
				PollNode.Name = Poll["polls_id"].ToString();
				PollsNode.Nodes.Add(PollNode);
				
				foreach(string key in Poll.Keys)
				{
					if(key != "entries")
					{
						TreeNode entryKeyNode = new TreeNode(key + " : " + Poll[key]);
						entryKeyNode.Name = key;
						PollNode.Nodes.Add(entryKeyNode);
					}
				}
				
				foreach(string key in Poll.Keys)
				{
					if(key == "entries")
					{
						ReplicaNode entriesNode = new ReplicaNode("entries");
						entriesNode.Name = "entries";
						PollNode.Nodes.Add(entriesNode);
						
						List<Dictionary<string,object>> entries = (List<Dictionary<string,object>>)Poll[key];
						
						foreach(Dictionary<string,object> entry in entries)
						{
							ReplicaNode entryNode = new ReplicaNode(entry["answers_title"].ToString());
							entryNode.Name = entry["polls_answers_id"].ToString();
							entriesNode.Nodes.Add(entryNode);
							
							foreach(string cle in entry.Keys)
							{
								TreeNode entryKeyNode = new TreeNode(cle + " : " + entry[cle]);
								entryKeyNode.Name = key;
								entryNode.Nodes.Add(entryKeyNode);
							}
						}
					}
				}
				
				store.PersistPoll(Poll);
				
				i++;
			}
			
			store.PersistPolls(Polls);
		}
		
		private void DownloadSlideImages(Site site,HtmlWeb client,TreeView treeView)
		{
			ReplicaNode SlideImagesNode = new ReplicaNode("SlideImages");
			SlideImagesNode.Name = "SlideImages";
			treeView.Nodes.Add(SlideImagesNode);
			
			SlideImageStore store = new SlideImageStore(site,client);
			List<SlideImage> SlideImages = store.GetItems();
			int i = 1;
			foreach(SlideImage slide_image in SlideImages)
			{
				ReplicaNode SlideImageNode = new ReplicaNode(slide_image.image_url);
				SlideImageNode.Name = slide_image.image_id;
				SlideImagesNode.Nodes.Add(SlideImageNode);
				
				i++;
			}
		}

		private void DownloadGuestBooks(Site site,HtmlWeb client,TreeView treeView)
		{
			ReplicaNode GuestBooksNode = new ReplicaNode("GuestBooks");
			GuestBooksNode.Name = "GuestBooks";
			treeView.Nodes.Add(GuestBooksNode);
			
			GuestBookStore store = new GuestBookStore(site,client);
			List<GuestBook> GuestBooks = store.GetItems();
			int i = 1;
			foreach(GuestBook guest_book in GuestBooks)
			{
				ReplicaNode GuestBookNode = new ReplicaNode(guest_book.title);
				GuestBookNode.Name = guest_book.guest_books_id;
				GuestBooksNode.Nodes.Add(GuestBookNode);
				
				i++;
			}
		}
		
		private void DownloadFaqs(Site site,HtmlWeb client,TreeView treeView)
		{
			ReplicaNode FaqsNode = new ReplicaNode("Faqs");
			FaqsNode.Name = "Faqs";
			treeView.Nodes.Add(FaqsNode);
			
			FaqStore store = new FaqStore(site,client);
			List<Faq> Faqs = store.GetItems();
			int i = 1;
			foreach(Faq faq in Faqs)
			{
				ReplicaNode FaqNode = new ReplicaNode(faq.faqs_question);
				FaqNode.Name = faq.faqs_id;
				FaqsNode.Nodes.Add(FaqNode);
				
				i++;
			}
		}
		
		private void DownloadArticles(Site site,HtmlWeb client,TreeView treeView)
		{
			ReplicaNode ArticlesNode = new ReplicaNode("Articles");
			ArticlesNode.Name = "Articles";
			treeView.Nodes.Add(ArticlesNode);
			
			ArticleStore store = new ArticleStore(site,client);
			List<Article> Articles = store.GetItems();
			int i = 1;
			foreach(Article article in Articles)
			{
				ReplicaNode ArticleNode = new ReplicaNode(article.articles_name);
				ArticleNode.Name = article.articles_id;
				ArticlesNode.Nodes.Add(ArticleNode);
				
				i++;
			}
		}
		
		private void DownloadProductAttachments(Site site,HtmlWeb client,TreeView treeView)
		{
			ReplicaNode productAttachmentsNode = new ReplicaNode("ProductAttachments");
			productAttachmentsNode.Name = "ProductAttachments";
			treeView.Nodes.Add(productAttachmentsNode);
			
			ProductAttachmentStore store = new ProductAttachmentStore(site,client);
			List<Dictionary<string,object>> productAttachments = store.GetItemAttachments();
			int i = 1;
			foreach(Dictionary<string,object> productAttachment in productAttachments)
			{
				ReplicaNode productAttachmentNode = new ReplicaNode(productAttachment["attachments_name"].ToString());
				productAttachmentNode.Name = productAttachment["attachments_id"].ToString();
				productAttachmentsNode.Nodes.Add(productAttachmentNode);
				
				foreach(string key in productAttachment.Keys)
				{
					TreeNode entryKeyNode = new TreeNode(key + " : " + productAttachment[key]);
					entryKeyNode.Name = key;
					productAttachmentNode.Nodes.Add(entryKeyNode);
				}
				
				store.PersistItemAttachment(productAttachment);
				
				i++;
			}
			
			store.PersistItemAttachments(productAttachments);
		}
		
		private void DownloadProducts(Site site,HtmlWeb client,TreeView treeView)
		{
			ReplicaNode ProductsNode = new ReplicaNode("Products");
			ProductsNode.Name = "Products";
			treeView.Nodes.Add(ProductsNode);
			
			ProductStore store = new ProductStore(site,client);
			List<Product> products = store.GetItems();
			
			int i = 1;
			foreach(Product product in products)
			{
				ReplicaNode productNode = new ReplicaNode(product.products_name);
				productNode.Name = product.products_id;
				ProductsNode.Nodes.Add(productNode);
				
				store.PersistItem(product);
				
				i++;
			}
			
			store.PersistItems(products);
		}
		
		private void DownloadSpecials(Site site,HtmlWeb client,TreeView treeView)
		{
			ReplicaNode SpecialsNode = new ReplicaNode("Specials");
			SpecialsNode.Name = "Specials";
			treeView.Nodes.Add(SpecialsNode);
			
			SpecialStore store = new SpecialStore(site,client);
			List<Dictionary<string,object>> specials = store.GetSpecials();
			int i = 1;
			foreach(Dictionary<string,object> special in specials)
			{
				ReplicaNode specialNode = new ReplicaNode(special["products_name"].ToString());
				specialNode.Name = special["specials_id"].ToString();
				SpecialsNode.Nodes.Add(specialNode);
				
				foreach(string key in special.Keys)
				{
					TreeNode entryKeyNode = new TreeNode(key + " : " + special[key]);
					entryKeyNode.Name = key;
					specialNode.Nodes.Add(entryKeyNode);
				}
				
				store.PersistSpecial(special);
				
				i++;
			}
			
			store.PersistSpecials(specials);
		}
		
		private void DownloadManufacturers(Site site,HtmlWeb client,TreeView treeView)
		{
			ReplicaNode ManufacturersNode = new ReplicaNode("Manufacturers");
			ManufacturersNode.Name = "Manufacturers";
			treeView.Nodes.Add(ManufacturersNode);
			
			ManufacturerStore store = new ManufacturerStore(site,client);
			List<Dictionary<string,object>> manufacturers = store.GetManufacturers();
			int i = 1;
			foreach(Dictionary<string,object> manufacturer in manufacturers)
			{
				ReplicaNode manufacturerNode = new ReplicaNode(manufacturer["manufacturers_name"].ToString());
				manufacturerNode.Name = manufacturer["manufacturers_id"].ToString();
				ManufacturersNode.Nodes.Add(manufacturerNode);
				
				foreach(string key in manufacturer.Keys)
				{
					TreeNode entryKeyNode = new TreeNode(key + " : " + manufacturer[key]);
					entryKeyNode.Name = key;
					manufacturerNode.Nodes.Add(entryKeyNode);
				}
				
				store.PersistManufacturer(manufacturer);
				
				i++;
			}
			
			store.PersistManufacturers(manufacturers);
		}
		
		private void DownloadQuantityDiscountGroups(Site site,HtmlWeb client,TreeView treeView)
		{
			ReplicaNode QuantityDiscountGroupNode = new ReplicaNode("QuantityDiscountGroup");
			QuantityDiscountGroupNode.Name = "QuantityDiscountGroup";
			treeView.Nodes.Add(QuantityDiscountGroupNode);
			
			QuantityDiscountGroupStore store = new QuantityDiscountGroupStore(site,client);
			List<Dictionary<string,object>> quantityDiscountGroups = store.GetQuantityDiscountGroups();
			int i = 1;
			foreach(Dictionary<string,object> group in quantityDiscountGroups)
			{
				ReplicaNode groupNode = new ReplicaNode(group["quantity_discount_groups_name"].ToString());
				groupNode.Name = group["quantity_discount_groups_id"].ToString();
				QuantityDiscountGroupNode.Nodes.Add(groupNode);
				
				foreach(string key in group.Keys)
				{
					if(key == "entries")
					{
						List<Dictionary<string,object>> entries = (List<Dictionary<string,object>>)group[key];
						
						foreach(Dictionary<string,object> entry in entries)
						{
							TreeNode entryNode = new TreeNode(entry["customers_groups_name"].ToString());
							entryNode.Name = entry["quantity_discount_groups_values_id"].ToString();
							groupNode.Nodes.Add(entryNode);
							
							foreach(string cle in entry.Keys)
							{
								TreeNode entryKeyNode = new TreeNode(cle + " : " + entry[cle]);
								entryKeyNode.Name = cle;
								entryNode.Nodes.Add(entryKeyNode);
							}
						}
					}
				}
				
				store.PersistQuantityDiscountGroup(group);
				
				i++;
			}
			
			store.PersistQuantityDiscountGroups(quantityDiscountGroups);
		}
		
		private void DownloadCustomerGroups(Site site,HtmlWeb client,TreeView treeView)
		{
			ReplicaNode CustomerGroupsNode = new ReplicaNode("CustomerGroups");
			CustomerGroupsNode.Name = "CustomerGroups";
			treeView.Nodes.Add(CustomerGroupsNode);
			
			CustomerGroupStore store = new CustomerGroupStore(site,client);
			List<Dictionary<string,object>> customerGroups = store.GetCustomerGroups();
			int i = 1;
			foreach(Dictionary<string,object> group in customerGroups)
			{
				ReplicaNode groupNode = new ReplicaNode(group["customers_groups_name"].ToString());
				groupNode.Name = group["customers_groups_id"].ToString();
				CustomerGroupsNode.Nodes.Add(groupNode);
				
				foreach(string key in group.Keys)
				{
					TreeNode groupKeyNode = new TreeNode(key + " : " + group[key]);
					groupKeyNode.Name = key;
					groupNode.Nodes.Add(groupKeyNode);
				}
				
				store.PersistCustomerGroup(group);
				
				i++;
			}
			
			store.PersistItemCustomerGroups(customerGroups);
		}
		
		private void DownloadAttributGroup(Site site,HtmlWeb client,TreeView treeView)
		{
			ReplicaNode AttributNode = new ReplicaNode("Attribut");
			AttributNode.Name = "Attribut";
			treeView.Nodes.Add(AttributNode);
			
			AttributStore store = new AttributStore(site,client);
			List<ProductAttributGroup> AttributGroups = store.GetItems();
			int i = 1;
			foreach(ProductAttributGroup group in AttributGroups)
			{
				ReplicaNode GroupeNode = new ReplicaNode(group.products_ProductAttributGroupes_groups_name);
				GroupeNode.Name = group.products_ProductAttributGroupes_groups_name;
				AttributNode.Nodes.Add(GroupeNode);
				
				store.PersistItem(group);
				
				i++;
			}
			
			store.PersistItems(AttributGroups);
		}
		
		private void DownloadProductVariantGroup(Site site,HtmlWeb client,TreeView treeView)
		{
			ReplicaNode ProductVariantNode = new ReplicaNode("ProductVariant");
			ProductVariantNode.Name = "ProductVariantNode";
			treeView.Nodes.Add(ProductVariantNode);
			
			ProductVariantGroupStore store = new ProductVariantGroupStore(site,client);
			List<ProductVariantGroup> productVariantGroups = store.GetItems();
			int i = 1;
			foreach(ProductVariantGroup group in productVariantGroups)
			{
				ReplicaNode GroupeNode = new ReplicaNode(group.products_variants_groups_name);
				GroupeNode.Name = group.products_variants_groups_name;
				ProductVariantNode.Nodes.Add(GroupeNode);
				
				store.PersistItem(group);
				
				i++;
			}
			
			store.PersistItems(productVariantGroups);
		}
		
		private void DownloadConfiguration(Site site,HtmlWeb client,TreeView treeView)
		{
			ReplicaNode configurationNode = new ReplicaNode("Configuration");
			configurationNode.Name = "Configuration";
			treeView.Nodes.Add(configurationNode);
			
			ConfigurationStore store = new ConfigurationStore(site,client);
			Dictionary<string,object> Configuration = store.GetConfiguration();
			
			foreach(string key in Configuration.Keys)
			{
				TreeNode configurationKeyNode = new TreeNode(key + " : " + Configuration[key]);
				configurationKeyNode.Name = key;
				configurationNode.Nodes.Add(configurationKeyNode);
			}
			
			store.PersistConfiguration(Configuration);
		}
		
		private void DownLoadCategories(Site site,HtmlWeb client,TreeView treeView)
		{
			ReplicaNode categoriesNode = new ReplicaNode("Categories");
			categoriesNode.Name = "categories";
			treeView.Nodes.Add(categoriesNode);
			
			CategoryStore store = new CategoryStore(site,client);
			List<Category> Categories = store.GetItems();
			
			int i = 1;
			foreach(Category category in Categories)
			{
				ReplicaNode categoryNode = new ReplicaNode(category.categories_name);
				categoryNode.Name = category.categories_id;
				categoriesNode.Nodes.Add(categoryNode);
				
				store.PersistItem(category);
				
				i++;
			}
			
			store.PersistItems(Categories);
		}
		
		void BtnCloseClick(object sender, EventArgs e)
		{
			Close();
		}
		
		private TreeNode GetNode(string name,TreeView treeView)
		{
			TreeNode _node;
			foreach(TreeNode node in treeView.Nodes)
			{
				treeView.SelectedNode = node;
				if(node.Name == name)
				{
					return node;
				}
				
				_node = GetNode(name,node);
				
				if(_node != null)
				{
					return _node;
				}
			}
			
			return null;
		}

		private TreeNode GetNode(string name,TreeNode node)
		{
			TreeNode _node;
			foreach(TreeNode node1 in node.Nodes)
			{
				node.TreeView.SelectedNode = node1;
				if(node1.Name == name)
				{
					return node;
				}
				
				_node = GetNode(name,node1);
				if(_node != null)
				{
					return _node;
				}
			}
			
			return null;
		}
		
		void BtnSynchronizeClick(object sender, EventArgs e)
		{
			Site siteSrc = _sites[comboBoxSrc.SelectedIndex];
			Site siteDest = _sites[comboBoxDest.SelectedIndex];
			
			if(siteSrc.Name != siteDest.Name)
			{
				try
				{
					BtnCompareClick(this,null);
					LanguageSynchronizer.Synchronize(siteSrc,siteDest,_client,(SyncDirection)comboBoxDirection.SelectedIndex);
					DeviseSynchronizer.Synchronize(siteSrc,siteDest,_client,(SyncDirection)comboBoxDirection.SelectedIndex);
					WeightClassSynchronizer.Synchronize(siteSrc,siteDest,_client,(SyncDirection)comboBoxDirection.SelectedIndex);
					UnitClassSynchronizer.Synchronize(siteSrc,siteDest,_client,(SyncDirection)comboBoxDirection.SelectedIndex);
					ArticleCategorySynchronizer.Synchronize(siteSrc,siteDest,_client,(SyncDirection)comboBoxDirection.SelectedIndex);
					InformationSynchronizer.Synchronize(siteSrc,siteDest,_client,(SyncDirection)comboBoxDirection.SelectedIndex);
					ArticleSynchronizer.Synchronize(siteSrc,siteDest,_client,(SyncDirection)comboBoxDirection.SelectedIndex);
					FaqSynchronizer.Synchronize(siteSrc,siteDest,_client,(SyncDirection)comboBoxDirection.SelectedIndex);
					SlideImageSynchronizer.Synchronize(siteSrc,siteDest,_client,(SyncDirection)comboBoxDirection.SelectedIndex);
					CategorySynchronizer.Synchronize(siteSrc,siteDest,_client,(SyncDirection)comboBoxDirection.SelectedIndex);
					BtnCompareClick(this,null);
					
				}
				catch (ArgumentOutOfRangeException)
				{
					MessageBox.Show("Invalid token");
				}
			}
			else
			{
				MessageBox.Show("The source and destination sites cannot be the same");
			}
		}
	}
}
